namespace Admin2025.Domain.Entities.App;

public class AppUser : EntityBase
{
    // 用户名
    public string Username { get; set; } = null!;
    // 密码
    public string Password { get; set; } = null!;
    // 盐，加密
    public string Salt { get; set; } = null!;
    public string? AvatarUrl { get; set; }

    // 昵称（可选）
    public string? Nickname { get; set; }
    // 用户拥有的角色集合
    public virtual ICollection<AppRole> Roles { get; set; } = new List<AppRole>();

    // 分配角色给用户（避免重复）
    public void AssignRole(AppRole role)
    {
        if (!Roles.Contains(role))
        {
            Roles.Add(role);
        }
    }
    // 移除用户的某个角色
    public void RemoveRole(AppRole role)
    {
        if (Roles.Contains(role))
        {
            Roles.Remove(role);
        }
    }
    public void ChangePassword(string oldPwdHash, string newPwdHash)
    {
        if (Password != oldPwdHash)
        {
            throw new Exception("旧密码不正确");
        }
        Password = newPwdHash;
    }

    public void UpdateNickname(string nickname) => Nickname = nickname;
    public void UpdateAvatar(string avatarUrl) => AvatarUrl = avatarUrl;
    public void Enable()
    {
        IsActive = true;
    }
    public void Disable() => IsActive = false;

    
}